package com.leve.control.controller;

import com.leve.control.constant.Constants;
import com.leve.control.constant.ErrorCode;
import com.leve.control.domain.BackUser;
import com.leve.control.service.BackUserService;
import com.leve.control.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  pc后台用户相关
 * @author whongyu
 * @create by 2019/6/12
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/backUser")
@Api(tags = {"pc:系统用户相关接口服务"})
public class BackUserController {

    @Autowired
    private BackUserService backUserService;

    @ApiOperation(value = "pc用户登录接口")
    @PostMapping(value = "/login")
    @ResponseBody
    public Result login(@RequestParam String userName,String password){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/backUser/login|pc用户登录接口,userName={},password={}: "+ userName,password);
        try{
            return backUserService.login(userName,password);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/backUser/login|pc用户登录接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "pc用户添加接口")
    @PutMapping(value = "/add")
    @ResponseBody
    public Result addBackUser(@RequestBody BackUser backUser){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/backUser/add|pc用户添加接口,backUser={}: "+ backUser);
        try{
            backUserService.addBackUser(backUser);
            return new Result(ErrorCode.SUCCESS);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/backUser/add|pc用户添加接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "pc用户编辑接口")
    @PostMapping(value = "/update")
    @ResponseBody
    public Result updateBackUser(@RequestBody BackUser backUser){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/backUser/update|pc用户编辑接口,backUser={}: "+ backUser);
        try{
            int i = backUserService.updateBackUser(backUser);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }else {
                return new Result(ErrorCode.E_20029);
            }

        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/backUser/update|pc用户编辑接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "pc用户删除接口")
    @DeleteMapping(value = "/remove")
    @ResponseBody
    public Result removeBackUser(@RequestParam int userId){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/backUser/remove|pc用户删除接口,userId={}: "+ userId);
        try{
            int i = backUserService.removeBackUser(userId);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }else {
                return new Result(ErrorCode.E_20029);
            }

        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/backUser/remove|pc用户删除接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "pc用户查询接口")
    @GetMapping(value = "/find")
    @ResponseBody
    public Result findBackUser(@RequestBody BackUser backUser){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/backUser/find|pc用户查询接口,backUser={}: "+ backUser);
        try{
            List<BackUser> users = backUserService.findBackUser(backUser);
            return new Result(ErrorCode.SUCCESS,users);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/backUser/find|pc用户查询接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }



}
